package cn.ac.jaas.isc.vegetable.pbms.service.impl;

import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.security.service.VegetableUser;
import cn.ac.jaas.isc.vegetable.common.security.util.SecurityUtils;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipment;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcEquipmentMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcManufacturer;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcManufacturerMapper;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcManufacturerService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 厂商表
 *
 * @author 张高昌
 * @date 2022-05-14 17:41:34
 */
@Slf4j
@Service
@AllArgsConstructor
public class ArcManufacturerServiceImpl extends ServiceImpl<ArcManufacturerMapper, ArcManufacturer> implements ArcManufacturerService {

    private final ArcEquipmentMapper arcEquipmentMapper;

    @Override
    public Page getArcManufacturerPage(Page page, ArcManufacturer arcManufacturer) {
        return baseMapper.selectPage(page, Wrappers.query(arcManufacturer));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveManufacturer(ArcManufacturer arcManufacturer) {
        ArcManufacturer nameModel = baseMapper.selectOne(Wrappers.<ArcManufacturer>query().lambda().eq(ArcManufacturer::getManufacturerName, arcManufacturer.getManufacturerName()));
        if (nameModel != null) {
            return R.failed("厂商名称已存在！");
        }
        ArcManufacturer codeModel = baseMapper.selectOne(Wrappers.<ArcManufacturer>query().lambda().eq(ArcManufacturer::getManufacturerCode, arcManufacturer.getManufacturerCode()));
        if (codeModel != null) {
            return R.failed("厂商编码已存在！");
        }
        VegetableUser user = SecurityUtils.getUser();
        arcManufacturer.setCreateTime(LocalDateTime.now());
        arcManufacturer.setCreateUser(user.getId());
        baseMapper.insert(arcManufacturer);
        return R.ok(true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateManufacturerById(ArcManufacturer arcManufacturer) {
        LambdaQueryWrapper<ArcManufacturer> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ArcManufacturer::getManufacturerName, arcManufacturer.getManufacturerName())
                .eq(ArcManufacturer::getType, arcManufacturer.getType())
                .ne(ArcManufacturer::getManufacturerId, arcManufacturer.getManufacturerId());
        ArcManufacturer nameModel = baseMapper.selectOne(wrapper);
        if (nameModel != null) {
            return R.failed("厂商名称已存在！");
        }
        LambdaQueryWrapper<ArcManufacturer> typeWrapper = Wrappers.lambdaQuery();
        typeWrapper.eq(ArcManufacturer::getManufacturerCode, arcManufacturer.getManufacturerCode())
                .ne(ArcManufacturer::getManufacturerId, arcManufacturer.getManufacturerId());
        ArcManufacturer codeModel = baseMapper.selectOne(typeWrapper);
        if (codeModel != null) {
            return R.failed("厂商编码已存在！");
        }
        VegetableUser user = SecurityUtils.getUser();
        arcManufacturer.setUpdateTime(LocalDateTime.now());
        arcManufacturer.setUpdateUser(user.getId());
        baseMapper.updateById(arcManufacturer);
        return R.ok(true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R removeManufacturerById(Integer manufacturerId) {
        ArcManufacturer arcManufacturer = baseMapper.selectById(manufacturerId);
        List<ArcEquipment> arcEquipments = arcEquipmentMapper.selectList(Wrappers.<ArcEquipment>query().lambda().eq(ArcEquipment::getManufacturerCode, arcManufacturer.getManufacturerCode()));
        if (arcEquipments.size() > 0) {
            return R.failed("该厂商已在使用中，无法删除");
        }
        baseMapper.deleteById(manufacturerId);
        return R.ok(true);
    }
}
